//
//  HZMallHomeSearchNaviBar.m
//  app
//
//  Created by 刘华舟 on 15/5/19.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZMallHomeSearchNaviBar.h"

#import "HZImageSourceConstant.h"

#import "HZTextAttachment.h"

#import "UIImage+Conveniently.h"

static const CGFloat kMallHomeLabelHeight = 30;
static const CGFloat kMallHomeMargin = 20;
static NSString * const kMallSearchText = @" 搜索商品";

@interface HZMallHomeSearchNaviBar()

@property (strong, nonatomic) UILabel *searchLabel;
@property (strong, nonatomic) UIButton *voiceBtn;


@end

@implementation HZMallHomeSearchNaviBar

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [HZ_MALL_MAIN_THEME_PURPLE colorWithAlphaComponent:0.0f];
        
        CGFloat marginTop =[self.ubo fh:UB_HEIGHT oh:-kMallHomeLabelHeight-20]/2;
        //label
        self.searchLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT ox:kMallHomeMargin fy:UB_TOP oy:kMallHomeMargin+marginTop],
                                                                   [self.ubo fw:UB_WIDTH ow:-2*kMallHomeMargin oh:kMallHomeLabelHeight])];
        self.searchLabel.textAlignment = NSTextAlignmentCenter;
        self.searchLabel.backgroundColor = [HZ_WHITE_COLOR colorWithAlphaComponent:1.0f];
        
        self.searchLabel.layer.masksToBounds = YES;
        self.searchLabel.layer.cornerRadius = [self.ubo uw:4];
        self.searchLabel.layer.borderColor = HZ_MALL_BORDER_LINE_GRAY_COLOR.CGColor;
        self.searchLabel.layer.borderWidth = [self.ubo pw:1];
        
        NSTextAttachment * textAttachment = [[NSTextAttachment alloc] init];//添加附件,图片
        
        UIImage *image = [UIImage imageNamed:kImageMallHomeSearch];
        
        textAttachment.image = image;
        
        textAttachment.bounds = CGRectMake(0, -2.5, image.size.height , image.size.width);
        
        NSAttributedString *imgString = [NSMutableAttributedString attributedStringWithAttachment:textAttachment];
        NSString *serachText = [SNUniteUrl shareUniteUrl].searchBoxText;
        if (!serachText.length) {
            serachText = kMallSearchText;
        }else{
            serachText = [NSString stringWithFormat:@" %@", serachText];
        }
        NSMutableAttributedString *atrString = [[NSMutableAttributedString alloc] initWithString:serachText];
        [atrString addAttribute:NSForegroundColorAttributeName value:HZ_MALL_TEXT_M_LIGHTGRAY_COLOR range:NSMakeRange(0, serachText.length)];
        [atrString addAttribute:NSFontAttributeName value:kHZFont_Content_M range:NSMakeRange(0, serachText.length)];
        [atrString insertAttributedString:imgString atIndex:0];
        
        self.searchLabel.attributedText = atrString;
        
        UITapGestureRecognizer *tapGestr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(searchViewOnClick)];
        [self.searchLabel addGestureRecognizer:tapGestr];
        self.searchLabel.userInteractionEnabled = YES;
        [self addSubview:self.searchLabel];
        
        //voiceBtn
        self.voiceBtn = [[UIButton alloc] initWithFrame:CGRectOf([self.searchLabel.ufo fx:UB_RIGHT ox:-kMallHomeLabelHeight fy:UB_TOP],
                                                                 [self.searchLabel.ubo ow:kMallHomeLabelHeight oh:kMallHomeLabelHeight])];
        [self.voiceBtn setImage:[UIImage imageNamed:kImageMallHomeVoice] forState:UIControlStateNormal];
        [self.voiceBtn addTarget:self action:@selector(voiceButtonOnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.voiceBtn];
        
    }
    return self;
}

- (void)searchViewOnClick{
    HZLog(@"搜索点击");
    if (self.delegate && [self.delegate respondsToSelector:@selector(mallHomeSearchTap)]) {
        [self.delegate mallHomeSearchTap];
    }
}

- (void)voiceButtonOnClick{
    HZLog(@"点击语音");
    if (self.delegate && [self.delegate respondsToSelector:@selector(mallHomeVoiceSearchTap)]) {
        [self.delegate mallHomeVoiceSearchTap];
    }
}

- (void)wxVoiceResultStr:(NSString *)str
{
    self.searchLabel.text = str;
}

- (void)mallHomeBackGroundAlphaChange:(CGFloat)alpha{
    self.backgroundColor = [HZ_MALL_MAIN_THEME_PURPLE colorWithAlphaComponent:alpha];
    //self.searchLabel.backgroundColor = [HZ_WHITE_COLOR colorWithAlphaComponent:alpha];
}

@end
